#include "DL_MD.h"
#include "DL_Service.h"
#include "PL.h"
#include "DL_OD.h"
#include "My_PARAM.h"
#include "Type_Define.h"
#include "Data_Buffer.h"
#include "DL_MH.h"
#include "DL_CM.h"
#include "Type_Define.h"
#include "DL_ISDU.h"
#include "DL_EH.h"
#include "PAGE_SPACE.h"
#include "DL_PD.h"
DL_MD_STATE_TYPE MD_STATE;

void INIT_DL_MD(void){
    MD_STATE=MD_Idle_0;
	MC_STRUCT.MC=&BasicBufferInOut[0]; //固定地址
    MC_STRUCT.CKT=&BasicBufferInOut[1];//固定地址
	 
	  tm_MaxCycleTime_us=ParaseMasterCycleTime();
	
	//初始化tm MaxUARTFrameTime，无需调用MaxUARTFrameTime，因为在HAL_UART_Receive中已经有超时时间
//	if(COM_SPEED==Speed_COM1){  //当速度为4.8kbit/s时
//		tm_MaxUARTframeTime_us=2500;//以us表示的传送一个字节的时间==Tbit*12
//	}else if(COM_SPEED==Speed_COM2){
//		tm_MaxUARTframeTime_us=240;//以240.48 us表示的传送一个字节的时间==Tbit*12
//	}else if(COM_SPEED==Speed_COM3){
//		tm_MaxUARTframeTime_us=52;//以50.08us表示的传送一个字节的时间==Tbit*12;
//	}
}

//初始化MC Struct
void INIT_MC_STRUCT(void){
   if(MD_STATE==MD_Startup_2||MD_STATE==MD_EstablishCOM_1||MD_STATE==MD_Idle_0){
        
        MC_STRUCT.OD=&BasicBufferInOut[2];		  
        MC_STRUCT.CKS=&BasicBufferInOut[3];
		   MC_STRUCT.OD_Len=1;
		   MC_STRUCT.RxBytes=2;//默认设置为方向为读的时候长度
		   MC_STRUCT.TxBytes=2; //同上
		   MC_STRUCT.SendBuffer=&BasicBufferInOut[2]; //t同上
        return;
   }
   if(MD_STATE==MD_PreOperate_3){
		   MC_STRUCT.OD=&BasicBufferInOut[2];
		   MC_STRUCT.OD_Len=ODLen_In_Pre;  //强制默认preOperate使用Type1_2 或者Type1_V
       MC_STRUCT.CKS=&BasicBufferInOut[2+ODLen_In_Pre]; //强制默认preOperate使用Type1_2 或者Type1_V
		 MC_STRUCT.RxBytes=2;//默认设置方向为读的时候长度
		  MC_STRUCT.TxBytes=ODLen_In_Pre+1;//同上
		  MC_STRUCT.SendBuffer=&BasicBufferInOut[2];//同上
	 
       return;
   }
   if(MD_STATE==MD_OPerate_4){
		   MC_STRUCT.PDint_Len= ParaseTableB6( ProcessDataIn);
		  MC_STRUCT.PDout_Len=ParaseTableB6(ProcessDataOut);
		   MC_STRUCT.OD=&BasicBufferInOut[MC_STRUCT.PDout_Len+2];
		  MC_STRUCT.OD_Len=ODLen_In_Operate;
		   MC_STRUCT.PDout=&BasicBufferInOut[2];
       
     
       MC_STRUCT.PDin=&BasicBufferInOut[2+MC_STRUCT.PDout_Len+ODLen_In_Operate];
       MC_STRUCT.CKS=&BasicBufferInOut[2+MC_STRUCT.PDout_Len+ODLen_In_Operate+ MC_STRUCT.PDint_Len];
        MC_STRUCT.RxBytes=2+MC_STRUCT.PDout_Len;
		  MC_STRUCT.TxBytes=MC_STRUCT.OD_Len+MC_STRUCT.PDint_Len+1;//同上
		  MC_STRUCT.SendBuffer=&BasicBufferInOut[2+MC_STRUCT.PDout_Len]; //同上
       return;
   }

}

void Event_PL_WakeUP(void){
    if(MD_STATE==MD_Idle_0){
        MD_STATE=MD_EstablishCOM_1;
        DL_MD_T1();
    }
}

void Event_tm_Tdsio(void){
    if(MD_STATE==MD_EstablishCOM_1){
        DL_MD_T10();
        MD_STATE=MD_Idle_0;
    }
}

//建立通信成功事件
void Event_Successful_COMx(void){
    if(MD_STATE==MD_EstablishCOM_1){
        DL_MD_T2();
        MD_STATE=MD_Startup_2;

    }
}


void Event_MCmd_PREOPRATE(void){
    if(MD_STATE==MD_Startup_2){
        DL_MD_T3();
        MD_STATE=MD_PreOperate_3;
    }
}

void Event_MCmd_OPERATE(void){
    if(MD_STATE==MD_PreOperate_3){
        DL_MD_T4();
        MD_STATE=MD_OPerate_4;
        return;
    }
}
 
void Event_MCmd_STARTULP(void){
    if(MD_STATE==MD_PreOperate_3){
        MD_STATE=MD_Startup_2;
        DL_MD_T6();
        return;
    }
    if(MD_STATE==MD_OPerate_4){
        MD_STATE=MD_Startup_2;
        DL_MD_T7();
        return;
    }  

}

void Event_MHInfo_ILLEGAL_MESSAGETYPE(void){
    if(MD_STATE==MD_PreOperate_3){
        MD_STATE=MD_Startup_2;
        DL_MD_T12();
        return;
    }
    if(MD_STATE==MD_OPerate_4){
        MD_STATE=MD_Startup_2;
        DL_MD_T11();
        return;
    }

}

void Event_MCmd_FALLBACK(void){
    if(MD_STATE==MD_PreOperate_3){
        PL_Start_FBDTimer( );
        return;
    }
    if(MD_STATE==MD_OPerate_4){
        PL_Start_FBDTimer( );
        return;
    }
}

 

void DL_MD_T1(void){
    INIT_MC_STRUCT();
   Event_MH_Conf_ACTIVE(); 
   Service_DL_Mode_ind(ESTABCOM);
   //开启定时器 Tdiso，使用TIM3
   PL_Start_TdisoTimer();
   
}


//此函数后，进入等待接受字节状态
void DL_MD_T2(void){
    INIT_MC_STRUCT();
    Event_OH_Conf_ACTIVE();
    Event_CH_Conf_ACTIVE();
        //访问物理芯片,得到当前速度
    Service_DL_Mode_ind(PL_Get_COMSpeed());
}

void DL_MD_T3(void){
   INIT_MC_STRUCT();
  
   Event_IH_Conf_ACTIVE();
   Event_EH_Conf_ACTIVE();
   Service_DL_Mode_ind(PREOPERATE);
}

void DL_MD_T4(void){
    INIT_MC_STRUCT();
    
    Event_PD_Conf_ACTIVE();
    Service_DL_Mode_ind(OPERATE);
}

void DL_MD_T6(void){
    INIT_MC_STRUCT();
    Event_IH_Conf_INACTIVE();
    Event_EH_Conf_INACTIVE();
    Service_DL_Mode_ind(STARTUP);
}

void DL_MD_T7(void){
    INIT_MC_STRUCT();
    Event_PD_Conf_INACTIVE();
    Event_IH_Conf_INACTIVE();
    Event_EH_Conf_INACTIVE();
    Service_DL_Mode_ind(STARTUP);
}

void DL_MD_T8_9(void){
    Event_PD_Conf_INACTIVE();
    Event_OH_Conf_INACTIVE();
    Event_CH_Conf_INACTIVE();
    Event_EH_Conf_INACTIVE();
    Event_IH_Conf_INACTIVE();
    Event_MH_Conf_INACTIVE();
    Service_DL_Mode_ind(INACTIVE);

}

void DL_MD_T10(void){
    Event_PD_Conf_INACTIVE();
    Event_OH_Conf_INACTIVE();
    Event_CH_Conf_INACTIVE();
    Event_EH_Conf_INACTIVE();
    Event_IH_Conf_INACTIVE();
    Event_MH_Conf_INACTIVE();
    Service_DL_Mode_ind(INACTIVE);
}


void DL_MD_T11(void){
    INIT_MC_STRUCT();
    Event_PD_Conf_INACTIVE();
    Event_IH_Conf_INACTIVE();
    Event_EH_Conf_INACTIVE();
    Service_DL_Mode_ind(STARTUP);
}

void DL_MD_T12(void){
    INIT_MC_STRUCT();
    Event_IH_Conf_INACTIVE();
    Event_EH_Conf_INACTIVE();
    Service_DL_Mode_ind(STARTUP);
}


